package com.jlxy.chen.eroad;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.EditText;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import base.BaseActivity;
import client.HttpUtil;

public class LoginActivity extends Activity implements OnClickListener {
//    private Button registbtn;
//    private Button loginbtn;
    EditText etMobile,etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_login);

        //获取按钮并监听
        Button registbtn = (Button)findViewById(R.id.login_btn_regist);
        registbtn.setOnClickListener(this);
        Button loginbtn = (Button)findViewById(R.id.login_btn_login);
        loginbtn.setOnClickListener(this);

        //获取输入框
        etMobile = (EditText)findViewById(R.id.login_moblie);
        etPassword = (EditText)findViewById(R.id.login_edit_pwd);






    }
    public void setActivity(Class<? extends Activity> tmpclass,Bundle bundle) {
        Intent intent = null;
        intent = new Intent(this,tmpclass);
        if(bundle!=null)
            intent.putExtras(bundle);
        this.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.login_btn_login:
                if(validate()){
                    if(loginPro()){
                        Intent intent = new Intent(LoginActivity.this,OrderActivity.class);
                        startActivity(intent);
                        finish();
                    }else{
                        DialogUtil.showDialog(LoginActivity.this,"用户名称或者密码错误",false);
                    }
                }

                break;
            case R.id.login_btn_regist:
                setActivity(RegistActivity.class,null);

                break;
        }

    }
    private boolean loginPro(){
        String username = etMobile.getText().toString();
        String pwd = etPassword.getText().toString();
        JSONObject jsonObject;
        try{
            jsonObject = query(username,pwd);
            if(jsonObject.getInt("userId")>0){
                return true;
            }
        }catch(Exception e){
            DialogUtil.showDialog(this,"服务器响应异常，请稍后再试",false);
            e.printStackTrace();
        }
        return false;
    }

    private JSONObject query(String username, String password)throws Exception {
        Map<String,String> map = new HashMap<>();
        map.put("user",username);
        map.put("pass",password);
        String url = HttpUtil.BASE_URL+"login.jsp";
        return new JSONObject(HttpUtil.postRequest(url,map));
    }

    private boolean validate(){
        String username = etMobile.getText().toString().trim();
        if(username.equals("")){
            DialogUtil.showDialog(this,"用户账户是必填的！",false);
            return false;
        }
        String pwd = etPassword.getText().toString().trim();
        if(pwd.equals("")){
            DialogUtil.showDialog(this,"用户密码是必填的！",false);
            return false;
        }
        return true;
    }


}
